package com.syqc.monitor.ui.carinfo.report

import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.HistoryAlarm
import com.syqc.monitor.R
import com.syqc.monitor.ui.alarm.ReportSetModel
import com.syqc.net.ServiceHelper
import com.syqc.utils.TimeUtil
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.*

/**
 *@Author Jasper
 *@Time   2020/10/15
 *@Desc   车辆模式报表
 */
class ReportAlarmViewModel : AutoDisposeViewModel() {
    var carId = ""
    var carName = ""

    //时间类型
    var timeType = 4

    //请求数据种类
    var isAdas = false

    //开始时间
    var startTime = ""

    //结束时间
    var endTime = ""

    //是否隐鲹
    var isHidden = false

    //已选
    var choose: String = ""

    //报警响应监听
    val alarmSub by lazy { PublishSubject.create<HistoryAlarm>() }


    fun getAlarmIds(isAdas: Boolean = false): String {
        return if (isAdas) {
            getHtml(
                choose,
                ReportSetModel.instance.getAdasLength(),
                ReportSetModel.instance.getAdasSize()
            )
        } else {
            getHtml(
                choose,
                ReportSetModel.instance.getNormalLength(),
                ReportSetModel.instance.getNormalSize()
            )
        }
    }

    private fun getHtml(desc: String, length: Int, size: Int) =
        "<font color='#FF333333' >$desc </font><font color='#2461f7'> $length</font><font color='#FF333333'>/$size</font>"


    /**
     * 获取时间
     */
    private fun startDayTime(day: Int): String {
        return "${
            TimeUtil.instance.simpleTime(Date().time - day * 24 * 3600000)
        } 00:00"
    }

    /**
     * 处理时间
     */
    fun handleTime() {
        when (timeType) {
            1 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getBeginDayOfYesterday().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getEndDayOfYesterday().time)

            }
            2 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getDayBegin().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getDayEnd().time)
            }
            3 -> {
                startTime = TimeUtil.instance.formatHms(startDayTime(2))
                endTime = TimeUtil.instance.getAllTime()
            }

            4 -> {
                startTime = TimeUtil.instance.formatHms(startDayTime(6))
                endTime = TimeUtil.instance.getAllTime()
            }
        }
        getData()
    }

    /**
     * 获取数据
     */
    private fun getData() {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }


        getAlarm()
    }


    /**
     * 查询报警数据
     */

    private fun getAlarm() {
        val ids = if (isAdas) ReportSetModel.instance.getAdasAlarmIds()
        else ReportSetModel.instance.getNormalAlarmIds()
        if (ids.isEmpty()) {
            toast(R.string.hint_choose_alarm_type)
            alarmSub.onNext(HistoryAlarm())
            return
        }
        toastLoading(true)
        ServiceHelper.getRepository().getHistoryAlarm(
            carId,
            startTime,
            endTime,
            ids,
            ReportSetModel.instance.getSpeed(isAdas),
            ReportSetModel.instance.getContinueTimeFormat(isAdas)
        ).autoDispose(this).subscribe({
            toastLoading(false)
            alarmSub.onNext(it)
        }, error)
    }


}
